package com.example.adminbase.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class QueryParam {
    /**
     * 页码, 从1开始
     */
    private Integer pageNum = 1;
    private Integer pageSize = 10;

    private List<String> tableSorts = new ArrayList<>();

    private boolean camelCaseToUnderline = true;

    public boolean isCamelCaseToUnderline() {
        return camelCaseToUnderline;
    }

    public void setCamelCaseToUnderline(boolean camelCaseToUnderline) {
        this.camelCaseToUnderline = camelCaseToUnderline;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public List<String> getTableSorts() {
        return tableSorts.stream().map(sortPattern -> {
            String[] patterns = sortPattern.split(" ");
            return humpToUnderline(patterns[0]) + " " + (patterns.length > 1 ? patterns[1] : "");
        }).collect(Collectors.toList());
    }

    public void setTableSorts(List<String> tableSorts) {
        this.tableSorts = tableSorts;
    }

    public String humpToUnderline(String para){
        StringBuilder sb=new StringBuilder(para);
        int temp=0;//定位
        for(int i=0;i<para.length();i++){
            if(Character.isUpperCase(para.charAt(i))){
                sb.insert(i+temp, "_");
                temp+=1;
            }
        }
        return sb.toString().toUpperCase();
    }
}
